package com.itheima.consumer.amqp;

import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenerSimpleQueueMessage(String message) { // 这里的类型是发的类型
        log.info("发送的数据为:{}",message);
        System.out.println(message);
//        throw new RuntimeException("故意的");  返回nack,mq接着尝试发送
        throw new MessageConversionException("故意的");  //返回reject,mq直接就删除消息了
    }

    @RabbitListener(queues = "work.queue")
    public void listenerWorkQueueMessage1(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.out.println("消费者1接收到:" + message);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = "work.queue")
    public void listenerWorkQueueMessage2(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.err.println("消费者2接收到:" + message);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = "queue")
    public void listenerFanoutQueueMessage1(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.out.println("消费者1接收到:" + message);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = "queue1")
    public void listenerFanoutQueueMessage2(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.err.println("消费者2接收到:" + message);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",durable = "true",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenerDirectQueueMessage1(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.out.println("消费者1接收到:" + message);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",durable = "true",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenerDirectQueueMessage2(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.err.println("消费者2接收到:" + message);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1",durable = "true"),
            exchange = @Exchange(name = "hmall.topic",durable = "true",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenerTopicQueueMessage1(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.out.println("消费者1接收到:" + message);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2",durable = "true"),
            exchange = @Exchange(name = "hmall.topic",durable = "true",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listenerTopicQueueMessage2(String message) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.err.println("消费者2接收到:" + message);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = "object.queue")
    public void listenerObjectQueue(Map<String,Object> map) { // 这里的类型是发的类型
//        log.info("发送的数据为:{}",message);
        System.err.println("消费者接收到:" + map);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = "deadLetter.queue")
    public void listenerDeadLetterQueue(Message message) { // 这里的类型是发的类型
        log.info("{}",new String(message.getBody(), StandardCharsets.UTF_8));
    }


    //基于插件的延迟消息交换机
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue",durable = "true"),
            exchange = @Exchange(name = "delay.direct",durable = "true",delayed = "true",type = ExchangeTypes.DIRECT),
            key = {"delay"}
    ))
    public void listenerDelayQueue(String message){
        log.info("{}",message);
    }

}

